package specialpersion

class SpecialPerson(var name: String)

class Student(var name: String)

class Older(var name: String)

class Worker(var name: String)

class House {
  def sale(specialPerson: SpecialPerson): Unit = {
    println("只给特殊人群买票:" + specialPerson.name)
  }
}

object SpecialPersonPredef {
  implicit  def otherToSpecialPerson(obj: Object): SpecialPerson = {
    if (obj.getClass == classOf[Student]) {
      val student = obj.asInstanceOf[Student]
      new SpecialPerson(student.name)
    } else if (obj.getClass == classOf[Older]) {
      val older = obj.asInstanceOf[Older]
      new SpecialPerson(older.name)
    }
    else {
      new SpecialPerson(null)
    }
  }
}

object SpecialPersonDemo {

  def main(args: Array[String]): Unit = {
    val special = new SpecialPerson("special")
    val student = new Student("studnet")
    val older = new Older("older")
    val worker = new Worker("worker")

    val house = new House()

    house.sale(special)
    import SpecialPersonPredef._
    house.sale(student)
    house.sale(older)
    house.sale(worker)
  }
}
